/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include "rtos-wsn.h"
#include "ARM/arm-common.h"


#define UTRSTAT_TX_EMPTY	(1 << 2)
#define UTRSTAT_RX_READY	(1 << 0)
#define UART_ERR_MASK		0xF 

#define DEFAULT_BAUDRATE	(115200)
#define SERIAL_CHAR_READY()     (rUTRSTAT0 & UTRSTAT_RX_READY)
#define SERIAL_READ_CHAR()      rURXH0
#define SERIAL_READ_STATUS()    (rUERSTAT0 & UART_ERR_MASK) 
#define SERIAL_WRITE_STATUS()	(rUTRSTAT0)
#define SERIAL_WRITE_READY()	((rUTRSTAT0) & UTRSTAT_TX_EMPTY)
#define SERIAL_WRITE_CHAR(c)	((rUTXH0) = (c))

/*
extern void CONTROL_DEMOSRC_STDinit(void);
extern void CONTROL_DEMOSRC_STDstart(void);
extern void CONTROL_DEMOSRC_STDstop(void);

extern void CONTROL_BLINKTEST_INinit(uint16, uint16);
extern void CONTROL_BLINKTEST_INstart(uint16);
extern void CONTROL_BLINKTEST_INstop(uint16);

*/

#ifdef _Debug_thread_

extern void CONTROL_THREADEVENT_TEINinit(uint16, uint16);
extern void CONTROL_THREADEVENT_TEINstart(uint16);
extern void CONTROL_THREADEVENT_TEINstop(uint16);


#define BLINK_TEST			1
#define BLINK_TO_RADIO  	2
#define THREAD_AND_EVENT	4
#endif

volatile uint16 test_state = 0;
uint8 wsn_app_flag = 0;

void isr_usart0_rx(void);


void usart0_init(void)
{
	int i;
	unsigned int reg = 0;    
   
    reg = 50000000 / (16 * DEFAULT_BAUDRATE) - 1;
	
    /* GPH9,GPH10 reserved. 
         * GPH8 = UEXTCLK GPH7 = nCTS1 GPH6 = nRTS1
	 * GPH5:GPH4 = UART1 GPH3:GPH2 = UART0
	 * GPH1 = nCTS0 GPH0 = nRTS0
        */
    rGPHCON = 0x003efaaa;
	/* Uart port pull-up disable */
    rGPHUP  = 0x000007ff;    
    /* FIFO disable, No Auto Flow Control */
	rUFCON0 = 0x00;
	rUMCON0 = 0x0;
	/* Normal,No parity,1 stop,8 bit */
	rULCON0 = 0x3;
	/*
	 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
	 * normal,interrupt or polling
	 */
	rUCON0 = 0x245;
	rUBRDIV0 = reg;
   
    for (i = 0; i < 100; i++)
    	;

	/* Enable INT_UART0 and INT_RXD0*/
    rINTMSK &= ~(BIT_UART0);
	rINTSUBMSK &= ~(BIT_SUB_RXD0);
}
static uint8 usart_value;
void isr_usart0_rx(void)
{
	uint8 value;

	/* receive a byte */
	value = SERIAL_READ_CHAR();	
	usart_value = value;

	/*clear interrupt request note: clearsubpending must before clear pending*/
	ClearSubPending(BIT_SUB_RXD0);	
	ClearPending(BIT_UART0);
	wsn_LeaveCritical();
	switch(value)
	{
	case '0':	// stop all
		/*
		if(test_state & BLINK_TEST){
			test_state &= ~BLINK_TEST;	
			CONTROL_BLINKTEST_INstop(0);//CONTROL_IN_IN(stop)->WSN_StopTimer
		}
		if(test_state & BLINK_TO_RADIO){
			test_state &= ~BLINK_TO_RADIO;	
			CONTROL_DEMOSRC_STDstop();//CONTROL_IN_STD(stop)
		}*/
#ifdef _Debug_thread_

		if(test_state & THREAD_AND_EVENT){
			test_state &= ~THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINstop(0);//CONTROL_IN_TEIN(stop)->WSN_ClearEvent
		}
#endif
		wsn_app_flag = 0;
		break;

/*
	case '1':	// start all
		if((test_state & BLINK_TEST) == 0){
			test_state |= BLINK_TEST; // blink test
			CONTROL_BLINKTEST_INinit(0, 0);//CONTROL_IN_IN(init)->WSN_InitTimer
			CONTROL_BLINKTEST_INstart(0); //CONTROL_IN_IN(start)->WSN_StartTimer
		}
		if((test_state & BLINK_TO_RADIO) == 0){
			test_state |= BLINK_TO_RADIO;
			CONTROL_DEMOSRC_STDinit();//CONTROL_IN_STD(init)
			CONTROL_DEMOSRC_STDstart();//CONTROL_IN_STD(start)
		}
		if((test_state & THREAD_AND_EVENT) == 0){
			test_state |= THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINstart(0);////CONTROL_IN_TEIN(start)->WSN_SetEvent
		}

		wsn_app_flag = 1;

		break;

	case '2':
		if((test_state & BLINK_TEST) == 0){
			test_state |= BLINK_TEST; // blink test
			CONTROL_BLINKTEST_INinit(0, 0);
			CONTROL_BLINKTEST_INstart(0);
		}
		break;

	case '3':					// blink to radio
		if((test_state & BLINK_TO_RADIO) == 0){
			test_state |= BLINK_TO_RADIO;
			CONTROL_DEMOSRC_STDinit();
			CONTROL_DEMOSRC_STDstart();
		}
		break;
		*/
#ifdef _Debug_thread_

	case '4':					// thread & event test
		if((test_state & THREAD_AND_EVENT) == 0){
			test_state |= THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINinit(0, 0);
			CONTROL_THREADEVENT_TEINstart(0);
		}
		break;
#endif		

	case '5':					// app test
		wsn_app_flag = 1;
		break;

	default:
		return;

}

}

/* output */

int dbgputs(char *nouse, char * str, int count)
{
    if (count == 0) {
		return 0;
    }
	
	while(count--)
	{
		while( !SERIAL_WRITE_READY() ) 
			;
		SERIAL_WRITE_CHAR(*str);
		
		str++;

	}
	return 0;
}

unsigned char console_check_input(void)
{
	return usart_value;
}

void console_input_clear(void)
{
	wsn_EnterCritical();
	usart_value = 0;
	wsn_LeaveCritical();
}

